package com.firemoss.ui.blanket.calloutClasses
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.events.CloseEvent;
	import mx.events.ResizeEvent;

	public class CalloutDisplay extends AbstractCallout
	{
		private var iconClass : Class
		
		public var icon : DisplayObject

		public var calloutCloseButton : Button
		
		[Embed("/com/firemoss/ui/blanket/calloutClasses/iconClose.png")]
		private var calloutCloseIcon : Class
		
		public var showCloseButton : Boolean = true
		
		public function get chrome() : CalloutBorder
		{
			return _chrome
		}
		private var _chrome : CalloutBorder = new CalloutBorder()
		
		override protected function createChildren():void
		{
			super.createChildren()
			
			iconClass = getStyle("icon") as Class
			
			addChild( chrome )
			
			if ( iconClass )
			{
				icon = new iconClass() as DisplayObject
				addChild( icon )
			}
			
			if ( showCloseButton )
			{
				calloutCloseButton = new Button()
				calloutCloseButton.width = 15
				calloutCloseButton.height = 15
				calloutCloseButton.setStyle("paddingLeft", 0)
				calloutCloseButton.setStyle("paddingRight", 0)
				calloutCloseButton.setStyle("paddingTop", 0)
				calloutCloseButton.setStyle("paddingBottom", 0)
				calloutCloseButton.setStyle("icon", calloutCloseIcon)
				
				
				addChild( calloutCloseButton )
				
				calloutCloseButton.addEventListener( MouseEvent.CLICK, closeButtonClicked, false, 0, true )
			}
			
			addChild( calloutContents )
			
			calloutContents.addEventListener( ResizeEvent.RESIZE, contentsResized, false, 0, true )
			
			chrome.callout = this
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList( unscaledWidth, unscaledHeight )
			
			var padding : Number = getStyle("padding")
			if ( isNaN(padding) )
			{
				padding = 3
			}	
			
			calloutContents.width = calloutContents.getExplicitOrMeasuredWidth()
			calloutContents.height = calloutContents.getExplicitOrMeasuredHeight()
			calloutContents.x = padding + chrome.cornerRadius
			calloutContents.y = unscaledHeight / 2 - calloutContents.height / 2
			
			if ( icon )
			{
				icon.y = padding - 3
				calloutContents.x += icon.width + padding - 3
			}
			
			if ( showCloseButton )
			{
				calloutCloseButton.visible = true
				calloutCloseButton.x = calloutContents.x + calloutContents.width + padding
				calloutCloseButton.y = padding
			}
			else if ( calloutCloseButton )
			{
				calloutCloseButton.visible = false
			}
			
			chrome.invalidateDisplayList()
			
			invalidateSize()
			invalidatePosition()
		}
		
		override protected function measure() : void
		{
			super.measure()
			
			var padding : Number = getStyle("padding")
			if ( isNaN(padding) )
			{
				padding = 3
			}	

			var w : Number = calloutContents.getExplicitOrMeasuredWidth() + padding * 2
			var h : Number = calloutContents.getExplicitOrMeasuredHeight() + padding * 2
			
			if ( icon )
			{
				w += icon.width + padding - 3
				
				if (icon.height > calloutContents.getExplicitOrMeasuredHeight())
				{
					h = icon.height + calloutContents.y * 2 - 3
				}
			}
			
			if ( showCloseButton && calloutCloseButton )
			{
				w += padding + calloutCloseButton.width + 10
			}
			
			measuredWidth = w
			measuredHeight = h
		}
		
		private function contentsResized( event : ResizeEvent ) : void
		{
			invalidatePosition()
			invalidateDisplayList()
		}
		
		private function closeButtonClicked( event : MouseEvent ) : void
		{
			dispatchEvent( new CloseEvent( CloseEvent.CLOSE, true ) )
		}
	}
}